package com.yvon.maple.service.auth.valicode.validate.code.processor;


import com.yvon.boot.common.result.GlobalException;
import com.yvon.maple.service.auth.enums.CodeTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 校验码处理器管理器
 * @author : Yvon
 * @since 2019/5/19
 */
@Component
public class CodeProcessorHolder {

	/**
	 * 依赖搜索
	 *
	 * Spring启动时，会查找容器中所有的ValidateCodeProcessor接口的实现，并把Bean的名字作为key，放到map中
	 */
	@Autowired
	private Map<String, CodeProcessor> processorPool;

	/**
	 * @param type
	 * @return
	 */
	public CodeProcessor getProcessor(String type) {
		CodeTypeEnum codeTypeEnum = CodeTypeEnum.getByType(type);
		if (codeTypeEnum == null) {
			throw new GlobalException("验证码处理器" + type + "不存在");
		}

		CodeProcessor processor = processorPool.get(codeTypeEnum.getProcessor());
		if (processor == null) {
			throw new GlobalException("验证码处理器" + type + "不存在");
		}
		return processor;
	}

}